package de.tiemannjan.android.util;

public class StopWatch extends NanoTime{    
	private long startTime;
    private boolean running;
       
    public StopWatch(){
    	super(0);
    	this.startTime = 0;
    	this.running = false;
    }
    
    public void start() {
        this.startTime = System.nanoTime();
        this.running = true;
    }

    
    public void stop() {
        this.update();
        this.running = false;
    }

    public void reset() {
        this.startTime = 0;
        this.setTime(0);
        this.running = false;
    }
    
    public void update(){
    	if (this.running){
    		long tmpTime = System.nanoTime();
    		long diff = tmpTime-this.startTime;
    		this.setTime(this.getTime() + diff);
    		this.startTime = tmpTime;
    	}
    }
    
    public long getMilliSeconds(){
    	this.update();
    	return super.getMilliSeconds();
    }
    
    public CharSequence getTimeFormatted(){
    	this.update();
    	return super.getTimeFormatted();
    }
}